//
//  PlayViewCtl.m
//  WYMusic
//
//  Created by wy on 16/9/16.
//  Copyright © 2016年 wy. All rights reserved.
//
//**歌单列表*/
// http://search.dongting.com/songlist/search?q=tag:%E6%9C%80%E7%83%AD&page=1  &size=10(分页)



#import "PlayViewCtl.h"
#import "NetworkRequest.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "MenuView.h"

#define SCREEN_WIDTH ( [[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ( [[UIScreen mainScreen] bounds].size.height)
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f \
blue:(b)/255.0f alpha:1.0f]


@interface PlayViewCtl ()<MenuViewDelegate>


@property(nonatomic,assign) float currentPlayTime;
@property(nonatomic,assign)BOOL isPlay;

@property(nonatomic,strong)MenuView *menuView;
@property (nonatomic, weak) UIButton *cover;

@end

@implementation PlayViewCtl

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.musicManager = [AVPlayerManager shareManager];
    
    //判断是否是从歌单列表进入
    if (self.musicManager.listInto == YES) {
        [self loadData];
    }else{
        [self.backgroudImageView sd_setImageWithURL:[NSURL URLWithString:self.musicManager.imageArray[self.index]] placeholderImage:[UIImage imageNamed:@"启动页"]];
        self.singerName.text = self.musicManager.singerArray[self.index];
        self.songsName.text = self.musicManager.songNameArray[self.index];
        self.isPlay =YES;
        [self playOrpause:self.playBtn];
    }
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playStutas:) name:@"playOrPasue" object:nil];
    
    [self.progressSlider setThumbImage:[UIImage imageNamed:@"music_slider_circle"] forState:UIControlStateNormal];
    [self.progressSlider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
    
    __weak typeof(self) weekSelf = self;
    //获取进度条以及播放时间
    self.musicManager.block1 = ^(NSString *current,NSString *total,float max , float value){
        weekSelf.currentLabel.text = current;
        weekSelf.totalLabel.text = total;
        weekSelf.progressSlider.maximumValue = max;
        weekSelf.progressSlider.minimumValue =0;
        weekSelf.progressSlider.value = value;
    } ;
    //获取缓冲状态
    self.musicManager.block = ^(NSString *loadTime){
        weekSelf.loadTimeLabel.text = loadTime;
    };
    //修改背景图，歌名、歌手
    self.musicManager.block2 = ^(NSInteger index){
        
        weekSelf.currentLabel.text = @"00:00";
        weekSelf.totalLabel.text = @"05:00";
        [weekSelf.backgroudImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",weekSelf.imageArray[index]]] placeholderImage:[UIImage imageNamed:@"启动页"]];
        weekSelf.songsName.text = weekSelf.songNameArray[index];
        weekSelf.singerName.text = weekSelf.singerArray[index];
    };
}

#pragma 音乐播放

- (IBAction)lastSong:(id)sender {
    self.isPlay = YES;
    [self playOrpause:self.playBtn];
    [self.musicManager lastSong];
}


- (IBAction)nestSong:(id)sender {
    self.isPlay = YES;
    [self playOrpause:self.playBtn];
    [self.musicManager nextSong];
}

#pragma 缓冲不足导致不能播放接受通知控制按钮状态
-(void)playStutas:(NSNotification *)notify{
    if ([[notify.userInfo valueForKey:@"播放状态"]  isEqual: @1]) {
        self.isPlay = YES;
        [self playOrpause:self.playBtn];
    }else if([[notify.userInfo valueForKey:@"播放状态"]  isEqual: @0]){
        self.isPlay = NO;
        [self playOrpause:self.playBtn];
    };
}

- (IBAction)playOrpause:(UIButton *)sender {
    if (self.isPlay == YES) {
        [self.musicManager.player play];
        [self.playBtn setImage:[UIImage imageNamed:@"player_pause"] forState:UIControlStateNormal];
    }else{
         [self.musicManager.player pause];
         [self.playBtn setImage:[UIImage imageNamed:@"player_play"] forState:UIControlStateNormal];
         NSLog(@"暂停");
    }
     self.isPlay = !self.isPlay;
}

-(void)sliderValueChange:(UISlider *)slider{
    [self.musicManager playerProgressWithProgressFloat:slider.value];
}


- (IBAction)menuBtn:(id)sender {
    [self createcover];
    self.menuView = [[MenuView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT/2) SongsArray:self.songNameArray playerTyper:self.musicManager.playerType];
    self.menuView.delegate = self;
    [self.view addSubview:self.menuView];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.cover.alpha = 0.5;
        self.menuView.frame = CGRectMake(0, SCREEN_HEIGHT/2, SCREEN_WIDTH, SCREEN_HEIGHT/2);
    }];
    
}

-(void)ClickSongsOfIndex:(NSInteger)index{
    [self.musicManager removeObserver];
    [self.musicManager musicPlayerWithIndex:index];
    
     self.isPlay =YES;
    [self.musicManager playMusic];
    
    [self returnMessage];
}

-(void)changPlayerType:(playerOfType)type{
    self.musicManager.playerType = type;
}


#pragma mark - 创建阴影
-(void)createcover{
    UIButton *cover = [[UIButton alloc] init];
    cover.frame = self.view.bounds;
    cover.backgroundColor = [UIColor blackColor];
    cover.alpha = 0.1;
    [cover addTarget:self action:@selector(returnMessage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cover];
    self.cover = cover;
}

-(void)returnMessage{
    [self removeView:self.menuView];
}

-(void)removeView:(UIView *)view{
    [UIView animateWithDuration:0.5 animations:^{
        self.cover.alpha =0.0;
        view.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 200);
    } completion:^(BOOL finished) {
        [view removeFromSuperview];
        [self.cover removeFromSuperview];
         self.cover  = nil;
    }];
}

- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma 数据请求
-(void)loadData{
        if (self.musicManager.musicArray.count <= 0) {
            [self.musicManager musicPlayerWithArray:self.songArray andIndex:self.index];
            self.musicManager.imageArray = self.imageArray;
            self.musicManager.singerArray = self.singerArray;
            self.musicManager.songNameArray = self.songNameArray;
            
            [self.backgroudImageView sd_setImageWithURL:[NSURL URLWithString:self.musicManager.imageArray[self.index]] placeholderImage:[UIImage imageNamed:@"启动页"]];
            self.singerName.text = self.musicManager.singerArray[self.index];
            self.songsName.text = self.musicManager.songNameArray[self.index];
            
        }else {
            
            if ([self.musicManager.musicArray indexOfObject:self.musicManager.player.currentItem] != self.index) {
                //让上一首歌播放时间回到起点！！！！
                [self.musicManager.player.currentItem seekToTime:kCMTimeZero];
            }
            [self.musicManager musicPlayerWithIndex:self.index];
            [self.backgroudImageView sd_setImageWithURL:[NSURL URLWithString:self.musicManager.imageArray[self.index]] placeholderImage:[UIImage imageNamed:@"启动页"]];
            self.singerName.text = self.musicManager.singerArray[self.index];
            self.songsName.text = self.musicManager.songNameArray[self.index];
            self.isPlay =YES;
            [self playOrpause:self.playBtn];
            
        }
}

@end
